package com.tel.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.my.ice.book.Message;
import com.my.ice.book.OnlineBookPrx;
import com.my.ice.tel.message.SMSService;
import com.zeroc.Ice.Communicator;
import com.zeroc.Ice.Current;
import com.zeroc.Ice.ObjectAdapter;
import com.zeroc.Ice.Object;
import com.zeroc.Ice.ObjectPrx;
import com.zeroc.Ice.Util;
import com.zeroc.IceBox.Service;

public class SMSServiceI implements SMSService, Service {
    private Logger log = LoggerFactory.getLogger(SMSServiceI.class);
    private ObjectAdapter _adapter;
    private static final long serialVersionUID = 1L;

    @Override
    public void sendSMs(String msg, Current current) {
        System.out.println("Send mesg" + msg);
        if (msg.startsWith("book")) {
            try {
                ObjectPrx base = _adapter.getCommunicator().stringToProxy("OnlineBook");

                OnlineBookPrx onlineBook = OnlineBookPrx.checkedCast(base);
                Message bookMsg = new Message();
                bookMsg.name = "Mr Wang";
                bookMsg.type = 3;
                bookMsg.price = 99.99;
                bookMsg.valid = true;
                bookMsg.content = "aaaa";
                onlineBook.bookTick(bookMsg);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }


    }

    @Override
    public void start(String name, Communicator communicator, String[] strings) {
        //创建ObjectAdapter, 这里和Service同名
        _adapter = communicator.createObjectAdapter(name);
        //创建servant 并激活
        Object object = this;
        _adapter.add(object, Util.stringToIdentity(name));
        _adapter.activate();
        log.trace("control","start");

    }

    @Override
    public void stop() {
        log.trace("control","stoped");
        _adapter.destroy();
    }
}
